﻿#region Using Statements
using System;
using Leap;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.GamerServices;
using RecognitionAPI;
#endregion

namespace ASL_BATTLE
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        SpriteFont font;
        DrawImages drawAllImages;
        KeyboardState _previousKeyboard;
        KeyboardState _keyboard;
        Player p = new Player();
        int counter = 0;
        //SignDetection ReadLetters = new SignDetection(true, false);
        string currentletter = null;

        //for picking new letters
        Boolean picked = false;

        //images 
        Texture2D[] images = new Texture2D[26];
        Texture2D character1;
        Rectangle char1 = new Rectangle(0, 100, 500, 500);
        Texture2D character2;
        Rectangle char2 = new Rectangle(510, 100, 500, 500);

        //Game States
        private enum GameState { Title, MainMenu, Game, Options, Credits };
        private GameState gState;

        //Menu Screens
        public int menuXloc;
        public int menuYloc;

        public int menuWidth;
        public int menuHeight;

        private Texture2D mainMenuTexture;
        private Rectangle mainMenu = new Rectangle(0, 0, 1024, 768);

        private Texture2D titleTexture;
        private Rectangle titleScreen = new Rectangle(0, 0, 1024, 768);

        private Texture2D gameTexture;
        private Rectangle gameBackground = new Rectangle(0,0,1024,768);


        Boolean write = true;
        int enemyHealth = 10;



        public Game1()
           : base()
        {
            graphics = new GraphicsDeviceManager(this);
            /*graphics.GraphicsDevice.PresentationParameters.BackBufferWidth = 1024;
            graphics.GraphicsDevice.PresentationParameters.BackBufferHeight = 768;
            graphics.GraphicsDevice.Viewport = new Viewport(0, 0, 1024, 768);
            graphics.ApplyChanges();*/
            graphics.PreferredBackBufferWidth = 1024;
            graphics.PreferredBackBufferHeight = 768;
            graphics.ApplyChanges();
            Content.RootDirectory = "Content";

            //Set the initial GameState, start in the title screen
            gState = GameState.Title;
            p = new Player();
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            
            //ReadLetters.Setup();
            base.Initialize();
            p.Init();
            LoadContent();
            pickLetter();
            picked = true;
            Console.WriteLine("Welcome to the fingerspelling game!");

        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            _keyboard = Keyboard.GetState();
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            font = Content.Load<SpriteFont>("Fonts/myFont");
            // TODO: use this.Content to load your game content here
            mainMenuTexture = Content.Load<Texture2D>("Menus/MenuBackground2.png");
            gameTexture = Content.Load<Texture2D>("Menus/background2.png");
            titleTexture = Content.Load<Texture2D>("Menus/TitleBackground2.png");

            character1 = Content.Load<Texture2D>("Characters/Character1.png");
            character2 = Content.Load<Texture2D>("Characters/Character2.png");

            images[0] = Content.Load<Texture2D>("ASL Signs/A.png");
            images[1] = Content.Load<Texture2D>("ASL Signs/B.png");
            images[2] = Content.Load<Texture2D>("ASL Signs/C.png");
            images[3] = Content.Load<Texture2D>("ASL Signs/D.png");
            images[4] = Content.Load<Texture2D>("ASL Signs/E.png");
            images[5] = Content.Load<Texture2D>("ASL Signs/F.png");
            images[6] = Content.Load<Texture2D>("ASL Signs/G.png");
            images[7] = Content.Load<Texture2D>("ASL Signs/H.png");
            images[8] = Content.Load<Texture2D>("ASL Signs/I.png");
            images[9] = Content.Load<Texture2D>("ASL Signs/J.png");
            images[10] = Content.Load<Texture2D>("ASL Signs/K.png");
            images[11] = Content.Load<Texture2D>("ASL Signs/L.png");
            images[12] = Content.Load<Texture2D>("ASL Signs/M.png");
            images[13] = Content.Load<Texture2D>("ASL Signs/N.png");
            images[14] = Content.Load<Texture2D>("ASL Signs/O.png");
            images[15] = Content.Load<Texture2D>("ASL Signs/P.png");
            images[16] = Content.Load<Texture2D>("ASL Signs/Q.png");
            images[17] = Content.Load<Texture2D>("ASL Signs/R.png");
            images[18] = Content.Load<Texture2D>("ASL Signs/S.png");
            images[19] = Content.Load<Texture2D>("ASL Signs/T.png");
            images[20] = Content.Load<Texture2D>("ASL Signs/U.png");
            images[21] = Content.Load<Texture2D>("ASL Signs/V.png");
            images[22] = Content.Load<Texture2D>("ASL Signs/W.png");
            images[23] = Content.Load<Texture2D>("ASL Signs/X.png");
            images[24] = Content.Load<Texture2D>("ASL Signs/Y.png");
            images[25] = Content.Load<Texture2D>("ASL Signs/Z.png");
                
            drawAllImages = new DrawImages(spriteBatch);

            

        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            _previousKeyboard = _keyboard;
            _keyboard = Keyboard.GetState();
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
                Exit();


            //Console.WriteLine(gState);

            //Switch for the game states: MainMenu, Game, Options, Credits
            switch (gState)
            {
                //Title Screen
                case GameState.Title:
                    IsMouseVisible = true;
                    
                    //Enter, spacebar, or mouse click moves to the main menu
                    if ((_keyboard.IsKeyDown(Keys.Enter) && _previousKeyboard.IsKeyUp(Keys.Enter)) || (_keyboard.IsKeyDown(Keys.Space) && _previousKeyboard.IsKeyUp(Keys.Space)) || Mouse.GetState().LeftButton == ButtonState.Pressed)
                    {
                        gState = GameState.MainMenu;
                    }

                    break;
                
                //Menus
                case GameState.MainMenu:
                    //Play Button
                    int pXPos = 850;
                    int pYPos = 270;
                    int pRectWidth = 170;
                    int pRectHeight = 60;

                    //Levels Button
                    int lXPos = 850;
                    int lYPos = 350;
                    int lRectWidth = 170;
                    int lRectHeight = 60;

                    //Options Button                                                       
                    int opXPos = 850;
                    int opYPos = 430;
                    int opRectWidth = 170;
                    int opRectHeight = 60;

                    //Credits Button                                                       
                    int crXPos = 850;
                    int crYPos = 520;
                    int crRectWidth = 170;
                    int crRectHeight = 60;

                    Rectangle spButton = new Rectangle(pXPos, pYPos, pRectWidth, pRectHeight);
                    Rectangle mpButton = new Rectangle(lXPos, lYPos, lRectWidth, lRectHeight);
                    Rectangle stButton = new Rectangle(opXPos, opYPos, opRectWidth, opRectHeight);
                    Rectangle cButton = new Rectangle(crXPos, crYPos, crRectWidth, crRectHeight);

                    //Temporary movement into the game loop
                    if ((_keyboard.IsKeyDown(Keys.Enter) && _previousKeyboard.IsKeyUp(Keys.Enter)) || (_keyboard.IsKeyDown(Keys.Space) && _previousKeyboard.IsKeyUp(Keys.Space)) || Mouse.GetState().LeftButton == ButtonState.Pressed)
                    {
                        gState = GameState.Game;
                    }


                    break;

                //Game
                case GameState.Game:
                    //Game loop


                    if (_keyboard.IsKeyDown(Keys.A) && _previousKeyboard.IsKeyUp(Keys.A))
                        {
                            if (currentletter == "A")
                            {
                                Console.WriteLine("You guessed correct!");
                                counter++;
                                picked = false;
                            }
                            else
                            {
                                p.takeDamage();
                            }
                        }
                    else if (_keyboard.IsKeyDown(Keys.B) && _previousKeyboard.IsKeyUp(Keys.B))
                        {
                            if (currentletter == "B")
                            {
                                Console.WriteLine("You guessed correct!");
                                counter++;
                                picked = false;
                            }
                            else
                            {
                                p.takeDamage();
                            }
                        }
                    else if (_keyboard.IsKeyDown(Keys.C) && _previousKeyboard.IsKeyUp(Keys.C))
                        {
                            if (currentletter == "C")
                            {
                                Console.WriteLine("You guessed correct!");
                                counter++;
                                picked = false;
                            }
                            else
                            {
                                p.takeDamage();
                            }
                        }
                    else if (_keyboard.IsKeyDown(Keys.D) && _previousKeyboard.IsKeyUp(Keys.D))
                        {
                            if (currentletter == "D")
                            {
                                Console.WriteLine("You guessed correct!");
                                counter++;
                                picked = false;
                            }
                            else
                            {
                                p.takeDamage();
                            }
                        }
                    else if (_keyboard.IsKeyDown(Keys.E) && _previousKeyboard.IsKeyUp(Keys.E))
                        {
                            if (currentletter == "E")
                            {
                                Console.WriteLine("You guessed correct!");
                                counter++;
                                picked = false;
                            }
                            else
                            {
                                p.takeDamage();
                            }
                        }
                    else if (_keyboard.IsKeyDown(Keys.F) && _previousKeyboard.IsKeyUp(Keys.F))
                        {
                            if (currentletter == "F")
                            {
                                Console.WriteLine("You guessed correct!");
                                counter++;
                                picked = false;
                            }
                            else
                            {
                                p.takeDamage();
                            }
                        }
                    else if (_keyboard.IsKeyDown(Keys.G) && _previousKeyboard.IsKeyUp(Keys.G))
                        {
                            if (currentletter == "G")
                            {
                                Console.WriteLine("You guessed correct!");
                                counter++;
                                picked = false;
                            }
                            else
                            {
                                p.takeDamage();
                            }
                        }
                    else if (_keyboard.IsKeyDown(Keys.H) && _previousKeyboard.IsKeyUp(Keys.H))
                        {
                            if (currentletter == "H")
                            {
                                Console.WriteLine("You guessed correct!");
                                counter++;
                                picked = false;
                            }
                            else
                            {
                                p.takeDamage();
                            }
                        }
                    else if (_keyboard.IsKeyDown(Keys.I) && _previousKeyboard.IsKeyUp(Keys.I))
                        {
                            if (currentletter == "I")
                            {
                                Console.WriteLine("You guessed correct!");
                                counter++;
                                picked = false;
                            }
                            else
                            {
                                p.takeDamage();
                            }
                        }
                    else if (_keyboard.IsKeyDown(Keys.J) && _previousKeyboard.IsKeyUp(Keys.J))
                        {
                            if (currentletter == "J")
                            {
                                Console.WriteLine("You guessed correct!");
                                counter++;
                                picked = false;
                            }
                            else
                            {
                                p.takeDamage();
                            }
                        }
                    else if (_keyboard.IsKeyDown(Keys.K) && _previousKeyboard.IsKeyUp(Keys.K))
                        {
                            if (currentletter == "K")
                            {
                                Console.WriteLine("You guessed correct!");
                                counter++;
                                picked = false;
                            }
                            else
                            {
                                p.takeDamage();
                            }
                        }
                    else if (_keyboard.IsKeyDown(Keys.L) && _previousKeyboard.IsKeyUp(Keys.L))
                        {
                            if (currentletter == "L")
                            {
                                Console.WriteLine("You guessed correct!");
                                counter++;
                                picked = false;
                            }
                            else
                            {
                                p.takeDamage();
                            }
                        }
                    else if (_keyboard.IsKeyDown(Keys.M) && _previousKeyboard.IsKeyUp(Keys.M))
                        {
                            if (currentletter == "M")
                            {
                                Console.WriteLine("You guessed correct!");
                                counter++;
                                picked = false;
                            }
                            else
                            {
                                p.takeDamage();
                            }
                        }
                    else if (_keyboard.IsKeyDown(Keys.N) && _previousKeyboard.IsKeyUp(Keys.N))
                        {
                            if (currentletter == "N")
                            {
                                Console.WriteLine("You guessed correct!");
                                counter++;
                                picked = false;
                            }
                            else
                            {
                                p.takeDamage();
                            }
                        }
                    else if (_keyboard.IsKeyDown(Keys.O) && _previousKeyboard.IsKeyUp(Keys.O))
                        {
                            if (currentletter == "O")
                            {
                                Console.WriteLine("You guessed correct!");
                                counter++;
                                picked = false;
                            }
                            else
                            {
                                p.takeDamage();
                            }
                        }
                    else if (_keyboard.IsKeyDown(Keys.P) && _previousKeyboard.IsKeyUp(Keys.P))
                        {
                            if (currentletter == "P")
                            {
                                Console.WriteLine("You guessed correct!");
                                counter++;
                                picked = false;
                            }
                            else
                            {
                                p.takeDamage();
                            }
                        }
                    else if (_keyboard.IsKeyDown(Keys.Q) && _previousKeyboard.IsKeyUp(Keys.Q))
                        {
                            if (currentletter == "Q")
                            {
                                Console.WriteLine("You guessed correct!");
                                counter++;
                                picked = false;
                            }
                            else
                            {
                                p.takeDamage();
                            }
                        }
                    else if (_keyboard.IsKeyDown(Keys.R) && _previousKeyboard.IsKeyUp(Keys.R))
                        {
                            if (currentletter == "R")
                            {
                                Console.WriteLine("You guessed correct!");
                                counter++;
                                picked = false;
                            }
                            else
                            {
                                p.takeDamage();
                            }
                        }
                    else if (_keyboard.IsKeyDown(Keys.S) && _previousKeyboard.IsKeyUp(Keys.S))
                        {
                            if (currentletter == "S")
                            {
                                Console.WriteLine("You guessed correct!");
                                counter++;
                                picked = false;
                            }
                            else
                            {
                                p.takeDamage();
                            }
                        }
                    else if (_keyboard.IsKeyDown(Keys.T) && _previousKeyboard.IsKeyUp(Keys.T))
                        {
                            if (currentletter == "T")
                            {
                                Console.WriteLine("You guessed correct!");
                                counter++;
                                picked = false;
                            }
                            else
                            {
                                p.takeDamage();
                            }
                        }
                    else if (_keyboard.IsKeyDown(Keys.U) && _previousKeyboard.IsKeyUp(Keys.U))
                        {
                            if (currentletter == "U")
                            {
                                Console.WriteLine("You guessed correct!");
                                counter++;
                                picked = false;
                            }
                            else
                            {
                                p.takeDamage();
                            }
                        }
                    else if (_keyboard.IsKeyDown(Keys.V) && _previousKeyboard.IsKeyUp(Keys.V))
                        {
                            if (currentletter == "V")
                            {
                                Console.WriteLine("You guessed correct!");
                                counter++;
                                picked = false;
                            }
                            else
                            {
                                p.takeDamage();
                            }
                        }
                    else if (_keyboard.IsKeyDown(Keys.W) && _previousKeyboard.IsKeyUp(Keys.W))
                        {
                            if (currentletter == "W")
                            {
                                Console.WriteLine("You guessed correct!");
                                counter++;
                                picked = false;
                            }
                            else
                            {
                                p.takeDamage();
                            }
                        }
                    else if (_keyboard.IsKeyDown(Keys.X) && _previousKeyboard.IsKeyUp(Keys.X))
                        {
                            if (currentletter == "X")
                            {
                                Console.WriteLine("You guessed correct!");
                                counter++;
                                picked = false;
                            }
                            else
                            {
                                p.takeDamage();
                            }
                        }
                    else if (_keyboard.IsKeyDown(Keys.Y) && _previousKeyboard.IsKeyUp(Keys.Y))
                        {
                            if (currentletter == "Y")
                            {
                                Console.WriteLine("You guessed correct!");
                                counter++;
                                picked = false;
                            }
                            else
                            {
                                p.takeDamage();
                            }
                        }

                    else if (_keyboard.IsKeyDown(Keys.Z) && _previousKeyboard.IsKeyUp(Keys.Z))
                        {
                            if (currentletter == "Z")
                            {
                                Console.WriteLine("You guessed correct!");
                                counter++;
                                picked = false;
                            }
                            else
                            {
                                p.takeDamage();
                            }
                        }





                        
                        if (p.getHealth() < 1)
                        {
                            Console.WriteLine("GAME OVER");
                            p.setHealth(10);
                            gState = GameState.MainMenu;
                        }
                        if (counter == 10)
                        {
                            Console.WriteLine("You are the mightiest wizard!");
                            gState = GameState.MainMenu;
                            counter = 0;
                            break;
                        }

                        if (picked == false)
                        {
                            
                            pickLetter();
                            picked = true;

                            write = true;
                            enemyHealth = (10 -counter);

                            Console.WriteLine("Enemy Health: " + enemyHealth);
                            Console.WriteLine("Your Magic Level: " + p.getHealth());
                            Console.WriteLine("Input the correct sign, based on the letter shown.");
                        }
                    break;

                //Options
                case GameState.Options:
                    //Options menu
                    // Similar to main menu
                    break;

                //Credits
                case GameState.Credits:
                    //Credits
                    // Button to escape to menu
                    break;

                //If they somehow break out, return them to the menu
                default:
                    gState = GameState.MainMenu;
                    break;

            }

            //Runs methods and code to update the program every loop
            //ReadLetters.SetLetter("A");
            //If signed letter is looked for letter:
            /*if (ReadLetters.currentLetter())
            {
                Console.WriteLine("A");
            }*/

            //keyboard input
           
            //Keys[] keys = Keyboard.GetState().GetPressedKeys();
            _keyboard = Keyboard.GetState();

            

            // TODO: Add your update logic here

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.AliceBlue);

            // TODO: Add your drawing code here
            spriteBatch.Begin();
            if (gState == GameState.Game)
            {
                spriteBatch.Draw(gameTexture, gameBackground, Color.White);

                spriteBatch.Draw(character1, char1, Color.White);
                spriteBatch.Draw(character2, char2, Color.White);

                drawAllImages.DrawSigns();

                int magic = 100;
                int health = 10;

                if (write == true)
                {
                    magic = p.getHealth();
                    health = enemyHealth;
                }
               
                spriteBatch.DrawString(font, "Magic: " + magic, new Vector2(100, 0), Color.Black);
                spriteBatch.DrawString(font, "Enemy's Health: " + health, new Vector2(700, 0), Color.Black);
            }
            if (gState == GameState.Title)
            {
                spriteBatch.Draw(titleTexture, titleScreen, Color.White);
            }

                //Main Menu Screen
            if (gState == GameState.MainMenu)
            {
                spriteBatch.Draw(mainMenuTexture, mainMenu, Color.White);
            }

            //Display images
            base.Draw(gameTime);
            spriteBatch.End();
        }
        

        public void pickLetter()
        {
            //Function to set current letter to a random letter.
            Random rand = new Random();
            int i = rand.Next(1, 27);
            switch (i)
            {
                case 1:
                    currentletter = "A";
                    drawAllImages.letter = images[0];
                    break;
                case 2:
                    currentletter = "B";
                    drawAllImages.letter = images[1];
                    break;
                case 3:
                    currentletter = "C";
                    drawAllImages.letter = images[2];
                    break;
                case 4:
                    currentletter = "D";
                    drawAllImages.letter = images[3];
                    break;
                case 5:
                    currentletter = "E";
                    drawAllImages.letter = images[4];
                    break;
                case 6:
                    currentletter = "F";
                    drawAllImages.letter = images[5];
                    break;
                case 7:
                    currentletter = "G";
                    drawAllImages.letter = images[6];
                    break;
                case 8:
                    currentletter = "H";
                    drawAllImages.letter = images[7];
                    break;
                case 9:
                    currentletter = "I";
                    drawAllImages.letter = images[8];
                    break;
                case 10:
                    currentletter = "J";
                    drawAllImages.letter = images[9];
                    break;
                case 11:
                    currentletter = "K";
                    drawAllImages.letter = images[10];
                    break;
                case 12:
                    currentletter = "L";
                    drawAllImages.letter = images[11];
                    break;
                case 13:
                    currentletter = "M";
                    drawAllImages.letter = images[12];
                    break;
                case 14:
                    currentletter = "N";
                    drawAllImages.letter = images[13];
                    break;
                case 15:
                    currentletter = "O";
                    drawAllImages.letter = images[14];
                    break;
                case 16:
                    currentletter = "P";
                    drawAllImages.letter = images[15];
                    break;
                case 17:
                    currentletter = "Q";
                    drawAllImages.letter = images[16];
                    break;
                case 18:
                    currentletter = "R";
                    drawAllImages.letter = images[17];
                    break;
                case 19:
                    currentletter = "S";
                    drawAllImages.letter = images[18];
                    break;
                case 20:
                    currentletter = "T";
                    drawAllImages.letter = images[19];
                    break;
                case 21:
                    currentletter = "U";
                    drawAllImages.letter = images[20];
                    break;
                case 22:
                    currentletter = "V";
                    drawAllImages.letter = images[21];
                    break;
                case 23:
                    currentletter = "W";
                    drawAllImages.letter = images[22];
                    break;
                case 24:
                    currentletter = "X";
                    drawAllImages.letter = images[23];
                    break;
                case 25:
                    currentletter = "Y";
                    drawAllImages.letter = images[24];
                    break;
                case 26:
                    currentletter = "Z";
                    drawAllImages.letter = images[25];
                    break;
                default:
                    break;
            }
        }
    }
}
